package com.wlb365.admin.modules.devops.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wlb365.admin.modules.devops.entity.RouteBase;
import com.wlb365.admin.modules.devops.entity.ServiceRoute;
import com.wlb365.admin.modules.devops.service.RouteBaseService;
import com.wlb365.admin.modules.devops.service.ServiceRouteService;
import com.wlb365.admin.properties.AdminProperties;
import com.wlb365.admin.properties.ServiceInfo;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.wlb365.admin.common.support.controller.BaseController;
import com.wlb365.admin.common.dto.R;

import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 * 路由配置 前端控制器
 * </p>
 *
 * @author
 * @since 2020-04-11 10:14:49
 */
@RestController
@RequestMapping("devops/serviceRoute")
public class ServiceRouteController extends BaseController {

    @Autowired
    private ServiceRouteService serviceRouteService;
    @Autowired
    private RouteBaseService routeBaseService;
    @Autowired
    private AdminProperties adminProperties;

    @RequiresPermissions("devops:serviceRoute:index")
    @GetMapping("index")
    public ModelAndView index() {
        return new ModelAndView("devops/serviceRoute");
    }

    @GetMapping(value = "list/data")
    public R listData(ServiceRoute serviceRoute) {
        Page<ServiceRoute> page = serviceRouteService.listServiceRoutePage(serviceRoute);
        return R.ok(page);
    }


    @RequiresPermissions("devops:serviceRoute:add")
    @PostMapping(value = "add")
    public R add(ServiceRoute serviceRoute) {
        serviceRouteService.save(serviceRoute);
        return R.ok();
    }

    @RequiresPermissions("devops:serviceRoute:del")
    @PostMapping(value = "batchdel")
    public R batchdel(@RequestParam("ids") List<Long> ids) {
        serviceRouteService.removeByIds(ids);
        return R.ok();
    }

    @RequiresPermissions("devops:serviceRoute:edit")
    @PostMapping(value = "edit")
    public R edit(ServiceRoute serviceRoute) {
        serviceRouteService.updateById(serviceRoute);
        return R.ok();
    }

    @RequiresPermissions("devops:serviceRoute:del")
    @PostMapping(value = "del/{id}")
    public R del(@PathVariable Long id) {
        serviceRouteService.removeById(id);
        return R.ok();
    }

    @GetMapping(value = "get/{id}")
    public R get(@PathVariable Long id) {
        return R.ok(serviceRouteService.getById(id));
    }

    @RequiresPermissions("devops:serviceRoute:enable")
    @PostMapping(value = "enable/{id}")
    public R enable(@PathVariable String id) {
        System.out.println("1启用");
        RouteBase routeBase = routeBaseService.getOne(new LambdaQueryWrapper<RouteBase>().eq(RouteBase::getRouteId, id));
        boolean doSave = routeBase == null;
        if (doSave) {
            routeBase = new RouteBase();
            routeBase.setRouteId(id);
        }
        routeBase.setStatus(1);
        boolean flag = doSave ? routeBaseService.save(routeBase) : routeBaseService.updateById(routeBase);
        if (flag) {
            System.out.println("启用成功");
        }

        return R.ok();
    }

    @RequiresPermissions("devops:serviceRoute:stop")
    @PostMapping(value = "stop/{id}")
    public R stop(@PathVariable String id) {
        //serviceRouteService.save(serviceRoute);
        System.out.println("禁用");
        RouteBase routeBase = routeBaseService.getOne(new LambdaQueryWrapper<RouteBase>().eq(RouteBase::getRouteId, id));
        boolean doSave = routeBase == null;
        if (doSave) {
            routeBase = new RouteBase();
            routeBase.setRouteId(id);
        }
        routeBase.setStatus(2);
        boolean flag = doSave ? routeBaseService.save(routeBase) : routeBaseService.updateById(routeBase);
        if (flag) {
            System.out.println("禁用成功");
        }
        return R.ok();
    }

}

